<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import ="com.easyattendance.services.AddSectionService" %>
<%@ page import ="com.easyattendance.model.Section" %>
<%@ page import ="com.easyattendance.services.AddClassService" %>
<%@ page import ="com.easyattendance.model.Class" %>
<%@ page import ="com.easyattendance.services.AddSubjectService" %>
<%@ page import ="com.easyattendance.model.Subject" %>
<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@ page import ="com.easyattendance.model.Teacher" %>

<html>
<head>
<title>Admin - Reports</title>

<jsp:include page="../includeFiles.jsp" ></jsp:include>

<link rel="stylesheet" href="<%=request.getContextPath()%>/views/css/ui/jquery-ui-1.10.4.custom.min.css">
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.core.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.widget.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.datepicker.js"></script>
<script>
	$(function() {
		$( "#from" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			dateFormat: 'yy-mm-dd',
			beforeShowDay: noSunday,
			onClose: function( selectedDate ) {
				$( "#to" ).datepicker( "option", "minDate", selectedDate );
			}
		});
		$( "#to" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			dateFormat: 'yy-mm-dd',
			beforeShowDay: noSunday,
			onClose: function( selectedDate ) {
				$( "#from" ).datepicker( "option", "maxDate", selectedDate );
			}
		});
	});
	function noSunday(date){ 
        return [date.getDay() != 0, ''];
    };
</script>
</head>

<!--
		Note: Set the body element's class to "left-sidebar" to position the sidebar on the left.
		Set it to "right-sidebar" to, you guessed it, position it on the right.
	-->
<body class="left-sidebar" onload="getValues()">

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp"></jsp:include>
<jsp:include page="../common/checkAdminRole.jsp" />

<%	String currentUser = (String) session.getAttribute("currentUser"); %>
	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">

				<!-- Generate Report-->
				<header>
				
				<% if(session.getAttribute("studentd_not_found")!=null && session.getAttribute("no_attendance_record")!=null){ %>  
					<span class="error_msg">${students_not_found}</span>
					<span class="error_msg">${no_attendance_record}</span>
					<% }%>	 
					<span class="error_msg">${null_values}</span>
					<span class="error_msg">${no_data_found}</span>
					<!--  <span class="success_msg">${lectureStatus}</span>	-->
					<div class="submit_errors_msg">
						<ul>
							<li id="submit_is_field_empty" class="submit_errors">All fields are required.</li>
						</ul>
					</div>
				
					<!-- Select Report Type -->
					<span class="byline">Select type to get particular reports.</span>
					<div id="report_type">
						<input type="radio" name="report_type" id="Attendance" value="Attendance" tabindex="1" checked>Attendance
						<input type="radio" name="report_type" id="Syllabus" value="Syllabus">Syllabus
						<input type="radio" name="report_type" id="Subject_Syllabus" value="Subject_Syllabus">Subject Wise Syllabus Status	
					</div>
					<span class="byline">All fields are required.</span>
					<!-- Get attendance report by entering following values. i.e. If Attendance Selected -->
					<form id="get_attendance" class="get_report" action="${pageContext.request.contextPath}/GetAdminReportsServlet" method="post" onsubmit="return att_checkFormErrors();">
					
					<!--  Hidden Fields -->
						<input type="hidden" name="action_type" value="attendance">
						
						<select id="att_select_section">
							<option value="">Select Section
								<%
								AddSectionService addSectionService = new AddSectionService();	
								List<Section> sectionList = new ArrayList<Section>();
								sectionList = addSectionService.listSections();
								Section tempSection = null;
								Iterator sectionItr = sectionList.iterator();
																							
							while(sectionItr.hasNext()) {
							%>
										<%
											tempSection = (Section) sectionItr.next();
											String sectionId = tempSection.getSection_id();
											String sectionName = tempSection.getSection_name();
										%>
										<option value=<%=sectionId%>><%=sectionName%>
							<% } %>
						</select>
						
						<% String sectionSelected = (String)request.getParameter("section");%>
						<input type="hidden" name="section_selected" value="<%=sectionSelected%>">
						
						<select id="att_select_class" name="att_select_class">
							<option value="">Select Class
								<%
								AddClassService addClassService = new AddClassService();	
								List<Class> classList = new ArrayList<Class>();
							    classList = addClassService.listClassBySectionId(sectionSelected);
								Class tempClass = null;
								Iterator classItr = classList.iterator();
																		
							while(classItr.hasNext()) {
							%>
								<%
									tempClass = (Class) classItr.next();
									String classId = tempClass.getClass_id();
								    String className = tempClass.getClass_name();
										%>
										<option value=<%=classId%>><%=className%>
									<%
										}
									%>
							
						</select>
						<input id="from" name="from" type="text" placeholder="From">
						<input id="to" name="to" type="text" placeholder="To">
						<input type="submit" value="Generate" class="button">
						
					</form>
						
					
					<!-- Get syllabus report by entering following values. i.e. If Syllabus Selected -->
					<form id="get_syllabus" class="get_report" action="${pageContext.request.contextPath}/GetAdminReportsServlet" method="post" onsubmit="return syll_checkFormErrors();">
						
						<!--  Hidden Fields -->
						<input type="hidden" name="action_type" value="syllabus">
						
						<select id="syll_select_section">
							<option value="">Select Section
								<%
								sectionItr = sectionList.iterator();
								while(sectionItr.hasNext()) { 
									tempSection = (Section) sectionItr.next();
									String sectionId = tempSection.getSection_id();
									String sectionName = tempSection.getSection_name();
							%>
										<option value=<%=sectionId%>><%=sectionName%>
									<%
										}
									%>
						</select>
						
						<% sectionSelected = (String)request.getParameter("section");%>
						<input type="hidden" name="section_selected" value="<%=sectionSelected%>">
						
						<select id="syll_select_class">
							<option value="">Select Class
								<%
								classItr = classList.iterator();
																		
							while(classItr.hasNext()) {
							%>
								<%
									tempClass = (Class) classItr.next();
									String classId = tempClass.getClass_id();
								    String className = tempClass.getClass_name();
										%>
										<option value=<%=classId%>><%=className%>
									<%
										}
									%>
							
						</select>
						
						<%String classSelected =(String) request.getParameter("class"); %>
						<input type="hidden" name="class_selected" value="<%=classSelected%>">
						
						<% if(classSelected!=null){ %>
							<!--  Select Teacher -->
							<select id="syll_select_teacher" name="syll_select_teacher" value="<script>getCookie("syll_select_teacher")</script>">
								<option value="Select Teacher">Select Teacher
									<% AddTeacherService addTeacherService = new AddTeacherService();	
										List<Teacher> teacherList = new ArrayList<Teacher>();
										teacherList = addTeacherService.listTeachers();
										Teacher tempTeacher = null;
										Iterator teacherItr = teacherList.iterator();
									
										while(teacherItr.hasNext()) { %>
											<% tempTeacher = (Teacher) teacherItr.next();
												String teacherId = tempTeacher.getTeacher_id();
												String teacherName = tempTeacher.getTeacher_name();%>
											<option value=<%=teacherId%>><%= teacherName %>
										<% } %>
							</select>
						<% } %>
						
						<% String teacherSelected = (String) request.getParameter("teacher"); %>
						
						<%if(teacherSelected!=null && classSelected!=null){
							
							AddSubjectService addSubjectService = new AddSubjectService();
							List<Subject> subjectList = new ArrayList<Subject>();
							subjectList = addSubjectService.listSubjectsByTeacherId(teacherSelected, classSelected);%>
							
							<%if( !subjectList.isEmpty()){%>
								<!--  Select Teacher Subject -->
								<select id="syll_select_subject" name="syll_select_subject">
									<option value="">Select Subject
										<% Subject tempSubject = null;
										Iterator subjectItr = subjectList.iterator();
										
										while(subjectItr.hasNext()) { %>
											<% tempSubject = (Subject) subjectItr.next();
											String subjectId = tempSubject.getSubject_id();
												String subjectName = tempSubject.getSubject_name();%>
											<option value=<%= subjectId %>><%= subjectName %>
										<% } %>	
								</select>
								<input type="hidden" name="teacher_selected" value="<%=teacherSelected%>">
								<input type="submit" value="Generate" class="button">
							<%} else {%>
								<div>
									<label> This Teacher do not teach any subject in this class</label>
								</div>
							<% } %>
						<%} %>
						
					</form>
					
					<!-- Get subject wise syllabus report by entering following values. i.e. If Subject_Syllabus Selected -->
					<form id="get_subject_syllabus" class="get_report" action="${pageContext.request.contextPath}/GetAdminReportsServlet" method="post"  onsubmit="return syll_sub_checkFormErrors();">
						
						<!--  Hidden Fields -->
						<input type="hidden" name="action_type" value="subject_syllabus">
						
						<select id="sub_syll_select_section" name="sub_syll_select_section" value="$(#sub_syll_select_section)">
							<option value="">Select Section
								<%
								sectionItr = sectionList.iterator();
								while(sectionItr.hasNext()) { 
									tempSection = (Section) sectionItr.next();
									String sectionId = tempSection.getSection_id();
									String sectionName = tempSection.getSection_name();
							%>
										<option value=<%=sectionId%>><%=sectionName%>
									<%
										}
									%>
						</select>
						
						<% sectionSelected = (String)request.getParameter("section");%>
						<input type="hidden" name="section_selected" value="<%=sectionSelected%>">
						
						<select id="sub_syll_select_class">
							<option value="">Select Class
								<%
								classItr = classList.iterator();
																		
							while(classItr.hasNext()) {
							%>
								<%
									tempClass = (Class) classItr.next();
									String classId = tempClass.getClass_id();
								    String className = tempClass.getClass_name();
										%>
										<option value=<%=classId%>><%=className%>
									<%
										}
									%>
							
						</select>
						
						<%-- <%String classSelected =(String) request.getParameter("class"); %> --%>
						<input type="hidden" name="class_selected" value="<%=classSelected%>">
						
						<% if(classSelected!=null){ %>
							<%-- <!--  Select Teacher -->
							<select id="sub_syll_select_teacher" name="sub_syll_select_teacher" value="<script>getCookie("sub_syll_select_teacher")</script>">
								<option value="Select Teacher">Select Teacher
									<% AddTeacherService addTeacherService = new AddTeacherService();	
										List<Teacher> teacherList = new ArrayList<Teacher>();
										teacherList = addTeacherService.listTeachers();
										Teacher tempTeacher = null;
										Iterator teacherItr = teacherList.iterator();
									
										while(teacherItr.hasNext()) { %>
											<% tempTeacher = (Teacher) teacherItr.next();
												String teacherId = tempTeacher.getTeacher_id();
												String teacherName = tempTeacher.getTeacher_name();%>
											<option value=<%=teacherId%>><%= teacherName %>
										<% } %>
							</select> --%>
						
						
						
							<% AddSubjectService addSubjectService = new AddSubjectService();
							List<Subject> subjectList = new ArrayList<Subject>();
							subjectList = addSubjectService.listSubjectsByClassId(classSelected);%>
							
							<%if( !subjectList.isEmpty()){%>
								<!--  Select Subject -->
								<select id="sub_syll_select_subject" name="sub_syll_select_subject">
									<option value="Select Subject">Select Subject
										<% Subject tempSubject = null;
										Iterator subjectItr = subjectList.iterator();
										
										while(subjectItr.hasNext()) { %>
											<% tempSubject = (Subject) subjectItr.next();
											String subjectId = tempSubject.getSubject_id();
												String subjectName = tempSubject.getSubject_name();%>
											<option value=<%= subjectId %>><%= subjectName %>
										<% } %>	
								</select>
								
								<input type="submit" value="Generate" class="button">
							<%} else {%>
								<div>
									<label> This Section do not have any subject in this class</label>
								</div>
							<% } %>
						<% } %>
						</form>
					
				</header>
				<footer>
						<span class="success_msg">${lectureStatus}</span>
						<span class="success_msg">${subjectLectureStatus}</span>
				</footer>
				<div id="login_content">
						
				</div>
				
			</div>
		</div>

		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<jsp:include page="../common/sidebarHeader.jsp" />

			<jsp:include page="adminMenu.jsp" />
							
			<!-- Sidebar Footer Content -->
			<jsp:include page="../common/sidebarFooter.jsp" />

		</div>

	</div>
	<script type="text/javascript">
		$(function(){
			$("#reports").addClass("current_page_item");
		});
		var contextPath='<%=request.getContextPath()%>';
	</script>
	
<script type="text/javascript">
	$("#get_syllabus").hide();
	$("#get_subject_syllabus").hide();
	$("#get_attendance").show();
	$("input[name='report_type']").click(function(){
		var report_type = $("input[name='report_type']:checked").val();
		if(report_type == "Attendance") {
			$("#get_syllabus").hide();
			$("#get_subject_syllabus").hide();
			$("#get_attendance").show();
		}else if (report_type == "Syllabus"){
			
			$("#get_attendance").hide();
			$("#get_subject_syllabus").hide();
			$("#get_syllabus").show();
		}
		else {
			$("#get_attendance").hide();
			$("#get_syllabus").hide();
			$("#get_subject_syllabus").show();
		}
	});
	
	$(".submit_errors_msg").hide();
	$(".submit_errors").hide();
	
	function att_checkFormErrors(){
		if(isAnyFieldEmpty($('#att_select_section'),$('#att_select_class'),$('#from'),$('#to'))){
			return true;
		}
		return false;
	}
	function syll_checkFormErrors(){
		if(isAnyFieldEmpty($('#syll_select_section'),$('#syll_select_class'),$('#syll_select_teacher'),$('#syll_select_subject'))){
			return true;
		}
		return false;
	}
	function sub_syll_checkFormErrors(){
		if(isAnyFieldEmpty($('#sub_syll_select_section'),$('#sub_syll_select_class'),$('#sub_syll_select_subject'))){
			return true;
		}
		return false;
	}
</script>
<!--  For drop down values -->
	<script type="text/javascript">
	
		$(function () {
		    var temp = 0;
		    $('#att_select_section').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var section = $("#att_select_section").val();
				setCookie("att_select_section",section);
				setCookie("is_syllabus","no");
				setCookie("is_subject_syllabus","no");
				window.location.replace(contextPath + "/views/admin/reports.jsp?section=" + section);
		    });      
		});
		
		
		$(function () {
		    var temp = 0;
		    $('#syll_select_section').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var section = $("#syll_select_section").val();
				setCookie("syll_select_section",section);
				setCookie("is_syllabus","1");
				setCookie("is_subject_syllabus","no");
				window.location.replace(contextPath + "/views/admin/reports.jsp?section=" + section);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#syll_select_class').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var section = $("#syll_select_section").val();
		    	var classs = $("#syll_select_class").val();
		    	setCookie("syll_select_section",section);
				setCookie("syll_select_class",classs);
				setCookie("is_syllabus","1");
				setCookie("is_subject_syllabus","no");
				window.location.replace(contextPath + "/views/admin/reports.jsp?section=" + section +"&class=" + classs);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#syll_select_teacher').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var section = $("#syll_select_section").val();
		    	var classs = $("#syll_select_class").val();
		    	var teacher = $("#syll_select_teacher").val();
		    	setCookie("syll_select_section",section);
				setCookie("syll_select_class",classs);
				setCookie("syll_select_teacher",teacher);
				setCookie("is_syllabus","1");
				setCookie("is_subject_syllabus","no");
				window.location.replace(contextPath + "/views/admin/reports.jsp?section="+section+"&class=" + classs + "&teacher=" + teacher);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#sub_syll_select_section').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var section = $("#sub_syll_select_section").val();
				setCookie("sub_syll_select_section",section);
				setCookie("is_syllabus","no");
				setCookie("is_subject_syllabus","1");
				window.location.replace(contextPath + "/views/admin/reports.jsp?section=" + section);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#sub_syll_select_class').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var section = $("#sub_syll_select_section").val();
		    	var classs = $("#sub_syll_select_class").val();
		    	setCookie("sub_syll_select_section",section);
				setCookie("sub_syll_select_class",classs);
				setCookie("is_syllabus","no");
				setCookie("is_subject_syllabus","1");
				window.location.replace(contextPath + "/views/admin/reports.jsp?section=" + section +"&class=" + classs);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#sub_syll_select_subject').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var section = $("#sub_syll_select_section").val();
		    	var classs = $("#sub_syll_select_class").val();
		    	var subject = $("#sub_syll_select_subject").val();
		    	setCookie("sub_syll_select_section",section);
				setCookie("sub_syll_select_class",classs);
				setCookie("sub_syll_select_subject",teacher);
				setCookie("is_syllabus","no");
				setCookie("is_subject_syllabus","1");
				window.location.replace(contextPath + "/views/admin/reports.jsp?section="+section+"&class=" + classs + "&subject=" + subject);
		    });      
		});
			
		function getValues(){
			$("#att_select_section").val(getCookie("att_select_section"));
			$("#syll_select_section").val(getCookie("syll_select_section"));
			$("#syll_select_class").val(getCookie("syll_select_class"));
			$("#syll_select_teacher").val(getCookie("syll_select_teacher"));
			$("#sub_syll_select_section").val(getCookie("sub_syll_select_section"));
			$("#sub_syll_select_class").val(getCookie("sub_syll_select_class"));
			$("#sub_syll_select_subject").val(getCookie("sub_syll_select_subject"));
			var isSyllabus = getCookie("is_syllabus");
			if(isSyllabus == "1"){
				$("#get_attendance").hide();
				$("#get_subject_syllabus").hide();
				$("#get_syllabus").show();
				document.getElementById("Syllabus").checked = true;
			}
			var isSubjectSyllabus = getCookie("is_subject_syllabus");
			if(isSubjectSyllabus == "1"){
				$("#get_attendance").hide();
				$("#get_syllabus").hide();
				$("#get_subject_syllabus").show();
				document.getElementById("Subject_Syllabus").checked = true;
			}
		}
		
		function getCookie(name) { 
			var re = new RegExp(name + "=([^;]+)"); 
			var value = re.exec(document.cookie);
			return (value != null) ? unescape(value[1]) : null; 
		}
		
		function setCookie(name, value) {
			  var today = new Date(); 
			  var expiry = new Date(today.getTime() + 1 * 24 * 3600 * 1000); // plus 30 days
			  document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
		 }
		
		function clearCookie(){
			deleteCookie("att_select_section");
			deleteCookie("syll_select_section");
			deleteCookie("syll_select_class");
			deleteCookie("syll_select_teacher");
			deleteCookie("is_syllabus");
			deleteCookie("sub_syll_select_section");
			deleteCookie("sub_syll_select_class");
			deleteCookie("sub_syll_select_subject");
			deleteCookie("is_subject_syllabus");
			alert("Cookies Deleted");
		}
		
		function deleteCookie(name)
		{
			var expired = new Date(today.getTime() - 24 * 3600 * 1000); // less 24 hours
			document.cookie=name + "=null; path=/; expires=" + expired.toGMTString();
		}		
	</script>
</body>


</html>